<?php
	get_header();
	
	$has_result = FALSE;
	
	$search_query = get_search_query();
	
	$page = $icl_utility->stripNonText($_GET['page']);
	$current_page = (isset($page) && $page != '') ? $page : 1;
	
	$max_per_page_product = 12;
	
	if ($search_query != '')
	{
		$sql = 'SELECT ' . WPSC_TABLE_PRODUCT_LIST . '.id, name, LEFT(description, 80) as description, image ';
		$sql .= 'FROM ' . WPSC_TABLE_PRODUCT_LIST . ' ';
		$sql .= 'WHERE publish = 1 AND active = 1 ';
		$sql .= 'AND (name LIKE "%'.$search_query.'%" OR description LIKE "%'.$search_query.'%" OR additional_description LIKE "%'.$search_query.'%" OR price LIKE "%'.$search_query.'%") ';
		
		$product_count = count($wpdb->get_results($sql, ARRAY_A));
		
		$sql .= 'LIMIT ' . (($current_page - 1) * $max_per_page_product) . ', ' . $max_per_page_product;
		
		$wpsc_query->products = $wpdb->get_results($sql, ARRAY_A);
		$wpsc_query->product_count = count($wpsc_query->products);
		
		$max_page = ceil($product_count / $max_per_page_product);
		$max_page = ($max_page == 0) ? 1 : $max_page;
		
		if ($wpsc_query->product_count > 0)
		{
			$has_result = TRUE;
		}
	}
	
?>
<script type="text/javascript" charset="utf-8">
/* <![CDATA[ */
function setEqualHeight(columns) {
	var tallestcolumn = 0;
	columns.each(
		function() {
			currentHeight = jQuery(this).height();
			if(currentHeight > tallestcolumn) {
				tallestcolumn  = currentHeight;
			}
		}
	);
	columns.css('min-height', tallestcolumn);
	delete tallestcolumn;
}

jQuery(document).ready(function() {
	setEqualHeight(jQuery(".wrapin  > div"));
});
/* ]]> */
</script>
	<div id="two-columns" class="container">
		<div class="content">
			<div id="right-column">
				<script type="text/javascript" charset="utf-8">
				/* <![CDATA[ */
				function setEqualHeight(columns) {
					var tallestcolumn = 0;
					columns.each(
						function() {
							currentHeight = jQuery(this).height();
							if(currentHeight > tallestcolumn) {
								tallestcolumn  = currentHeight;
							}
						}
					);
					columns.css('min-height', tallestcolumn);
					delete tallestcolumn;
				}

				jQuery(document).ready(function() {
					setEqualHeight(jQuery(".wrapin  > div"));
				});
				/* ]]> */
				</script>
				<div class="product-listing-top">
					<h3>Search Results</h3>
					<div class="listing-top-right">
						<p><?php _e('Page'); ?> <?php echo $current_page ?> <?php _e('of'); ?> <label id="sys_max_number_page"><?php echo $max_page ?></label></p>
						<p>
							<?php if ($current_page == 1) : ?>
							<label>< <?php _e('Previous'); ?></label>
							<?php else: ?>
							<a href="<?php echo get_option('siteurl'); ?>/?s=<?php echo $search_query; ?>&page=<?php echo ($current_page - 1); ?>">< <?php _e('Previous'); ?></a>
							<?php endif; ?>
							|
							<label id="sys_label_next_page" style="<?php if ($current_page == $max_page){}else{ echo 'display:none;'; } ?>"><?php _e('Next'); ?> ></label>
							<a id="sys_link_next_page" style="<?php if ($current_page == $max_page){ echo 'display:none;'; } ?>" href="<?php echo get_option('siteurl'); ?>/?s=<?php echo $search_query; ?>&page=<?php echo ($current_page + 1); ?>"><?php _e('Next'); ?> ></a>
						</p>
					</div><!-- .listing-top-right -->
					<div class="clear"></div>
				</div><!-- .product-listing-top -->
				<ul class="product-listing">
					<li class="clear"></li>
					<?php while (wpsc_have_products()) :  wpsc_the_product(); ?>
					<li class="product-li">
						<div class="list-image">
						<?php if(get_option('show_thumbnails')) :?>
						<?php $product_image = wpsc_the_product_image(); ?>
						<?php if($product_image != '') :?>
							<a href="<?php echo wpsc_the_product_permalink(); ?>">
								<img id="product_image_<?php echo wpsc_the_product_id(); ?>" alt="<?php echo wpsc_the_product_title(); ?>" title="<?php echo wpsc_the_product_title(); ?>" src="<?php bloginfo('stylesheet_directory'); ?>/libraries/timthumb.php?w=145&src=<?php echo urlencode($product_image); ?>" />
							</a>
						<?php else: ?>
							<span>No Image Available</span>
						<?php endif; ?>
						<?php endif; ?>
						</div><!-- .list-image -->
						<div class="list-desc">
							<h5>
								<?php if(get_option('hide_name_link') == 1) : ?>
									<span><?php echo wpsc_the_product_title(); ?></span>
								<?php else: ?> 
									<a href="<?php echo wpsc_the_product_permalink(); ?>"><?php echo wpsc_the_product_title(); ?></a>
								<?php endif; ?>
							</h5>
							<p class="list-category">
								<?php echo $icl_utility->output_get_categories(wpsc_the_product_id(), ', ', '<a href="%s">', '</a>'); ?>
							</p><!-- .list-category -->
							<p class="list-price">
								<?php if(wpsc_product_is_donation()) : ?>
								<?php else : ?>
									<?php if(wpsc_product_on_special()) : ?>
										<span style="text-decoration:line-through;"><?php echo wpsc_product_normal_price(); ?></span>
									<?php endif; ?>
									<?php echo wpsc_the_product_price(); ?>
								<?php endif; ?>
							</p><!-- .list-price -->
							<p class="list-description">
								<?php echo wpsc_the_product_description(); ?>
							</p><!-- .list-description -->
							<div class="more">
								<a href="<?php echo wpsc_the_product_permalink(); ?>"><?php _e('More Information'); ?></a>
							</div><!-- .more -->
						</div><!-- .list-desc -->
					</li><!-- .product-li -->
					<?php endwhile; ?>
					<li class="clear"></li>
				</ul><!-- .product-listing -->
				<div class="product-listing-bottom">
					<div class="listing-bottom-right">
						<p><?php _e('Page'); ?> <?php echo $current_page ?> <?php _e('of'); ?> <?php echo $max_page ?></p>
						<p>
							<?php if ($current_page == 1) : ?>
							<label>< <?php _e('Previous'); ?></label>
							<?php else: ?>
							<a href="<?php echo get_option('siteurl'); ?>/?s=<?php echo $search_query; ?>&page=<?php echo ($current_page - 1); ?>">< <?php _e('Previous'); ?></a>
							<?php endif; ?>
							|
							<?php if ($current_page == $max_page) : ?>
							<label><?php _e('Next'); ?> ></label>
							<?php else: ?>
							<a href="<?php echo get_option('siteurl'); ?>/?s=<?php echo $search_query; ?>&page=<?php echo ($current_page + 1); ?>"><?php _e('Next'); ?> ></a>
							<?php endif; ?>
						</p>
					</div><!-- .listing-bottom-right -->
					<div class="clear"></div>
				</div><!-- .product-listing-bottom -->
			</div><!-- #right-column -->
			<?php get_sidebar(); ?>
			<div class="clear"></div>
		</div><!-- .content -->
	</div><!-- #two-columns -->
	
<?php get_footer(); ?>